// pages/automobile/add.js
const request = require('../../../utils/request'); // 引入封装好的请求工具
const userjs = require('../../../utils/user'); // 引入封装好的请求工具
const options = [
  {
    text: '小型车',
    value: 1,
  },
  {
    text: '大型车',
    value: 2,
  },
  {
    text: '电动车',
    value: 3,
  },
  {
    text: '摩托车',
    value: 4,
  },
  {
    text: '三轮车',
    value: 5,
  },
  {
    text: '其他车',
    value: 6,
  },
];
const options2 = [
  {
    "text": "白色",
    "value": "白色"
},
{
    "text": "灰色",
    "value": "灰色"
},
{
    "text": "黄色",
    "value": "黄色"
},
{
    "text": "粉色",
    "value": "粉色"
},
{
    "text": "红色",
    "value": "红色"
},
{
    "text": "紫色",
    "value": "紫色"
},
{
    "text": "绿色",
    "value": "绿色"
},
{
    "text": "蓝色",
    "value": "蓝色"
},
{
    "text": "棕色",
    "value": "棕色"
},
{
    "text": "黑色",
    "value": "黑色"
},
{
    "text": "其他",
    "value": "其他"
}
];

const options3 = [
  {
    "text": "小型汽车",
    "value": 1
  },
  {
    "text": "大型汽车",
    "value": 3
  },
  {
    "text": "新能源",
    "value": 2
  },
  {
    "text": "香港车辆",
    "value": 4
  },
  {
    "text": "澳门车辆",
    "value": 5
  },
  {
    "text": "外籍车辆",
    "value": 6
  },
  {
    "text": "摩托车",
    "value": 7
  },
];

const options4 = [
  {
    "text": "蓝色",
    "value":"蓝色"
},
{
    "text": "白色",
    "value":"白色"
},
{
    "text": "黄色",
    "value":"黄色"
},
{
    "text": "黑色",
    "value":"黑色"
},
{
    "text": "农用绿",
    "value":"农用绿"
},
{
    "text": "黄绿",
    "value":"黄绿"
},
{
    "text": "绿色",
    "value":"绿色"
},
{
    "text": "其他",
    "value":"其他"
}
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vehicleTypeMap: {
      1: "小型车",
      2: "大型车",
      3: "电动车",
      4: "摩托车",
      5: "三轮车",
      6: "其他车"
    },
    indexMap:{
      "小型车":1,
      "大型车":2,
      "电动车":3,
      "摩托车":4,
      "三轮车":5,
      "其他车":6,
    },
    brand:'',
    plate:'',
    show: false,
    show2: false,
    show3: false,
    show4: false,
    options,
    options2,
    options3,
    options4,
    fieldValue: '',
    cascaderValue: '',
    fieldValue2: '',
    cascaderValue2: '',
    fieldValue3: '',
    cascaderValue3: '',
    fieldValue4: '',
    cascaderValue4: '',
    fileList: [

    ],
    fileList2: [],
    addV:[],
    loading:false,
    myuser:null
  },
  onChange(e){
    console.log(e.detail);
    const { value, callback } = e.detail;
    this.setData({
      plate:value
    })
    
  },

  onChangebrand(e){
    console.log(e.detail);
    const { value, callback } = e.detail;
    this.setData({
      brand:value
    })

  },

  onClick() {
    this.setData({
      show: true,
    });
  },
  onClick2() {
    this.setData({
      show2: true,
    });
  },
  onClick3() {
    this.setData({
      show3: true,
    });
  },
  onClick4() {
    this.setData({
      show4: true,
    });
  },

  onClose() {
    this.setData({
      show: false,
    });
  },
  onClose2() {
    this.setData({
      show2: false,
    });
  },
  onClose3() {
    this.setData({
      show3: false,
    });
  },
  onClose4() {
    this.setData({
      show4: false,
    });
  },

  onFinish(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      fieldValue,
      cascaderValue: value,
    })
  },

  onFinish2(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue2 = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      fieldValue2,
      cascaderValue2: value,
    })
  },
  onFinish3(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue3 = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      fieldValue3,
      cascaderValue3: value,
    })
  },
  onFinish4(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue4 = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      fieldValue4,
      cascaderValue4: value,
    })
  },
  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.data });
        this.setData({ fileList });
      },
    });
  },
  afterRead2(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList2 = [] } = this.data;
        fileList2.push({ ...file, url: res.data });
        this.setData({ fileList2 });
      },
    });
  },

  saveC() {
    const url = '/jy/vehicle_manage';
    const { fieldValue, brand, fieldValue2, fieldValue3, plate, fieldValue4 } = this.data;

    this.setData({
      loading: true
    });

    // 校验各个字段是否为空
    if (!fieldValue) {
      wx.showToast({
        title: '车辆类型不能为空',
        icon: 'none',
      });
      return;
    }
    if (!brand) {
      wx.showToast({
        title: '车辆品牌不能为空',
        icon: 'none',
      });
      return;
    }
    if (!fieldValue2) {
      wx.showToast({
        title: '车辆颜色不能为空',
        icon: 'none',
      });
      return;
    }
    if (!fieldValue3) {
      wx.showToast({
        title: '车牌类型不能为空',
        icon: 'none',
      });
      return;
    }
    if (!plate) {
      wx.showToast({
        title: '车牌号不能为空',
        icon: 'none',
      });
      return;
    }
    if (!fieldValue4) {
      wx.showToast({
        title: '请选其他必要信息',
        icon: 'none',
      });
      return;
    }
    const { cascaderValue,cascaderValue2,cascaderValue3,cascaderValue4,myuser} = this.data
    
    console.log({"type":cascaderValue, brand, "color":fieldValue2, "numberType":cascaderValue3, "plateNumber":plate, "plateColor":cascaderValue4 },myuser);
    request.post(url, {"type":cascaderValue, brand, "color":fieldValue2, "numberType":cascaderValue3, "plateNumber":plate, "plateColor":cascaderValue4 ,"userId": myuser.userId})
      .then(response => {
        console.log(response);
        if (response.code === 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          });
          // 页面跳转或其他逻辑
          wx.navigateBack();
        }
      })
      .catch(error => {
        console.error('添加失败:', error);
        this.setData({
          loading: false
        });
      });

    // 在此处执行保存逻辑
  },


  

  // /**
  //  * 生命周期函数--监听页面加载
  //  */
  // onLoad(options) {
  //   const url = '/jy/vehicle_manage/{id}';
  //   request.post(url).then(response => {
  //     this.setData({
  //       Vinfo: response.rows.filter(item=>item.userId == this.data.myuser.userId)
  //     })
  //     console.log(response);
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示，每次页面更新都会触发！
   */
  onLoad() {
    // 如果都通过校验，则可以保存数据
    userjs.getUserProfile().then(res=>{
      console.log(res);
       this.setData({
         myuser: res.data
       })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})